﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Threading;

#endregion

namespace SSATool.Common.Util.ExtensionMethods
{
    /// <summary>
    /// Contains the global utilities for the ReaderWriterLock class.
    /// Adds a AcquireReaderLock / AcquireWriterLock method in order to
    /// use this method with the using() C# statement.
    /// </summary>
    public static class ReaderWriterLockUtil
    {
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Acquires a reader lock with an infinite time-out.
        /// </summary>
        public static IDisposable AcquireReaderLock(this ReaderWriterLock left)
        {
            return new ReaderLockDisposable(left);
        }

        /// <summary>
        /// Acquires a writer lock with an infinite time-out.
        /// </summary>
        public static IDisposable AcquireWriterLock(this ReaderWriterLock left)
        {
            return new WriterLockDisposable(left);
        }

        #endregion

        private class ReaderLockDisposable : DisposeBase
        {
            #region Declarations
            //--------------------------------------------------------------------
            // Declarations
            //--------------------------------------------------------------------

            private readonly ReaderWriterLock _parent;
            
            #endregion

            #region Constructors / Destructor
            //--------------------------------------------------------------------
            // Constructors / Destructor
            //--------------------------------------------------------------------

            internal ReaderLockDisposable(ReaderWriterLock parent)
            {
                PreCondition.AssertNotNull(parent, "parent");

                _parent = parent;
                _parent.AcquireReaderLock(Timeout.Infinite);
            }

            #endregion

            #region Methods
            //--------------------------------------------------------------------
            // Methods
            //--------------------------------------------------------------------

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                _parent.ReleaseReaderLock();
            }

            #endregion
        }

        private class WriterLockDisposable : DisposeBase
        {
            #region Declarations
            //--------------------------------------------------------------------
            // Declarations
            //--------------------------------------------------------------------

            private readonly ReaderWriterLock _parent;

            #endregion

            #region Constructors / Destructor
            //--------------------------------------------------------------------
            // Constructors / Destructor
            //--------------------------------------------------------------------

            internal WriterLockDisposable(ReaderWriterLock parent)
            {
                PreCondition.AssertNotNull(parent, "parent");

                _parent = parent;
                _parent.AcquireWriterLock(Timeout.Infinite);
            }

            #endregion

            #region Methods
            //--------------------------------------------------------------------
            // Methods
            //--------------------------------------------------------------------

            /// <summary>
            /// Releases managed (c# or .net) resources.
            /// </summary>
            protected override void ReleaseManagedResources()
            {
                _parent.ReleaseWriterLock();
            }

            #endregion
        }
    }
}
